{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### numpy用法详解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://repo.huaweicloud.com/repository/pypi/simple\n",
      "Requirement already satisfied: numpy in c:\\programdata\\miniconda3\\lib\\site-packages (1.19.3)\n"
     ]
    }
   ],
   "source": [
    "# 安装numpy\n",
    "\n",
    "# 使用pip安装numpy(通用、快捷)\n",
    "!pip3 install numpy\n",
    "\n",
    "# linux使用包管理器安装numpy\n",
    "# Ubuntu/Debian使用\n",
    "# sudo apt-get install python3-numpy\n",
    "# centos/redhat使用\n",
    "# sudo yum install python3-numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 验证numpy是否安装成功\n",
    "import numpy as np\n",
    "print(np.eye(3))  # 输出3*3的单位的对角矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### array函数的参数介绍\n",
    "---\n",
    "numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)\n",
    "- object: 可以是任何可迭代对象，比如list、tuple、range等\n",
    "- dtype: 指定数组的数据类型，默认为None，表示自动推断\n",
    "- copy: 指定是否复制数据，默认为True\n",
    "- order: 指定数组的存储顺序，默认为None，表示自动推断\n",
    "- subok: 指定是否允许访问子数组，默认为False\n",
    "- ndmin: 指定数组的最小维度，默认为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[[1 2]\n",
      " [3 4]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 2, 3]) # 创建一个一维数组\n",
    "b = np.array([[1, 2], [3, 4]]) # 创建一个2*2的数组\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### numpy的数据类型\n",
    "> numpy的数据类型也是array函数的参数dtype的类型.\n",
    "---\n",
    "- bool_ :bool类型\n",
    "- int_ :默认的整数类型\n",
    "- intc :与c的int类型一样,一般是int32或int64\n",
    "- intp :用于索引的整数类型，一般是int32或int64\n",
    "- int8 :字节(-128~127)\n",
    "- int16 :整数(-32768~32767)\n",
    "- int32 :整数(-2147483648~2147483647)\n",
    "- int64 :整数(-9223372036854775808~9223372036854775807)\n",
    "- uint8 :无符号整数(0~255)\n",
    "- uint16 :无符号整数(0~65535)\n",
    "- uint32 :无符号整数(0~4294967295)\n",
    "- uint64 :无符号整数(0~18446744073709551615)\n",
    "- float_ :float64类型的简写\n",
    "- float16 :半精度浮点数\n",
    "- float32 :单精度浮点数\n",
    "- float64 :双精度浮点数\n",
    "- complex_ :complex128类型的简写\n",
    "- complex64 :单精度复数\n",
    "- complex128 :双精度复数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 数据类型对象dtype\n",
    "---\n",
    "> 字节顺序是通过对数据类型预先设定<或>来指定的,<最小值存储在最小地址,>重要字节存储在最小地址\n",
    "numpy.dtype(object,align=False,copy=False)\n",
    "- object: 要转换为的数据类型对象\n",
    "- align: 如果为True,填充字段类似c的结构体\n",
    "- copy: 复制dtype对象,如果为False,则对内置数据类型对象的引用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "uint64\n",
      "float64\n",
      "complex64\n"
     ]
    }
   ],
   "source": [
    "# 数据类型可以使用相应的字符串来代替,int8,int16,int32,int64可分别用字符串'i1','i2','i4','i8'表示,i后面的数字乘以8,对应的就是字节数\n",
    "print(np.dtype('i8')) # 输出int64\n",
    "print(np.dtype('u8')) # 输出uint64\n",
    "print(np.dtype('f8')) # 输出float64\n",
    "print(np.dtype('c8')) # 输出complex64\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(10, b'Guido') (20, b'Steve')]\n"
     ]
    }
   ],
   "source": [
    "# 创建结构化数据类型\n",
    "dt = np.dtype([('age', np.int8), ('name','S10')]) \n",
    "\n",
    "# 将数据类型应用于数组对象\n",
    "a = np.array([(10, b'Guido'), (20, b'Steve')], dtype=dt) \n",
    "\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 内建类型都有一个唯一定义它的字符代码\n",
    "- 字符b 对应的是bool_\n",
    "- 字符i 对应的是int_整数型\n",
    "- 字符u 对应的是uint_无符号整数型\n",
    "- 字符f 对应的是float_浮点型\n",
    "- 字符c 对应的是complex_复数浮点型\n",
    "- 字符O 对应的是object_对象\n",
    "- 字符S,a 对应的是bytes_字符串\n",
    "- 字符U 对应的是unicode_字符串\n",
    "- 字符V 对应的是void_空类型\n",
    "- 字符M 对应的是datetime64[D]_日期时间型\n",
    "- 字符m 对应的是timedelta64[D]_时间型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ecf5722fdaf1897a315d257d89d94520bfcaa453217d5becf09b39e73618b0de"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
